package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.link.view.adapter.PickFilterWindowAdapter;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

/*
 * 三级筛选
 * 仅适用三级联动数据列表
 * 仅适用单选模式
 * 初始位置内容自定义设置
 * 勾选模式可通过方法设置
 * 返回数据为PickResultEntity 封装每层所选数据
 * 重置按钮恢复至初始状态并且回调重置方法返给用户处理(自动关闭window)
 * 待完善
 * */
public class ThreeLevelFilterWindow extends BasePopWindow {

    private PickFilterWindowAdapter pickAdapter1;
    private PickFilterWindowAdapter pickAdapter2;
    private PickFilterWindowAdapter pickAdapter3;
    private RecyclerView recycler1;
    private RecyclerView recycler2;
    private RecyclerView recycler3;
    private PickResultEntity pickResultEntity1;
    private PickResultEntity pickResultEntity2;
    private PickResultEntity pickResultEntity3;

    private String all = "全部";

    private List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> list;
    private int currentPosition = 0;
    private int twoPosition = 0;
    private int threePosition = 0;

    public ThreeLevelFilterWindow(Context context) {
        super(context);
    }

    public ThreeLevelFilterWindow(Context context, String all) {
        super(context);
        this.all = all;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_filter_packers_3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recycler1 = holder.getPop().findViewById(R.id.rlv_list);
        recycler2 = holder.getPop().findViewById(R.id.rlv2_list);
        recycler3 = holder.getPop().findViewById(R.id.rlv3_list);
        TextView reset = holder.getPop().findViewById(R.id.reset);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);

        //重置
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickResultEntity1.clear();
                pickResultEntity2.clear();
                pickResultEntity3.clear();
                pickAdapter1.setDefItem(0);
                pickAdapter2.setDefItem(0);
                pickAdapter3.setDefItem(0);
                recycler1.scrollToPosition(0);
                recycler2.setVisibility(View.GONE);
                recycler3.setVisibility(View.GONE);
                if (onResultClick != null) {
                    onResultClick.reset();
                }
                dissMiss();
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(pickResultEntity1, pickResultEntity2, pickResultEntity3);
                    dissMiss();
                }
            }
        });

        if (pickAdapter1 == null) {
            pickAdapter1 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window, null);
            pickAdapter1.setIsDisplay(false);
            pickAdapter1.setItemSelect(true);
            pickAdapter1.setDefItem(0);
        }
        if (pickAdapter2 == null) {
            pickAdapter2 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window_1, null);
            pickAdapter2.setIsDisplay(true);
            pickAdapter2.setItemSelect(true);
            pickAdapter2.setDefItem(0);
        }
        if (pickAdapter3 == null) {
            pickAdapter3 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window_1, null);
            pickAdapter3.setIsDisplay(true);
            pickAdapter3.setItemSelect(true);
            pickAdapter3.setDefItem(0);
        }

        XLinearLayoutManager manager = new XLinearLayoutManager(mContext);
        recycler1.setLayoutManager(manager);
        recycler1.setAdapter(pickAdapter1);
        XLinearLayoutManager manager1 = new XLinearLayoutManager(mContext);
        recycler2.setLayoutManager(manager1);
        recycler2.setAdapter(pickAdapter2);
        XLinearLayoutManager manager2 = new XLinearLayoutManager(mContext);
        recycler3.setLayoutManager(manager2);
        recycler3.setAdapter(pickAdapter3);

        pickAdapter1.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                currentPosition = position;
                pickResultEntity1.setNo(entity.getNo());
                pickResultEntity1.setName(entity.getName());
                if (list.get(position).getChildren() != null) {
                    pickResultEntity2.setNo(list.get(position).getChildren().get(0).getNo());
                    pickResultEntity2.setName(list.get(position).getChildren().get(0).getName());
                    if (list.get(position).getChildren().get(0).getChildren() != null) {
                        pickResultEntity3.setNo(list.get(position).getChildren().get(0).getChildren().get(0).getNo());
                        pickResultEntity3.setName(list.get(position).getChildren().get(0).getChildren().get(0).getName());
                    } else {
                        pickResultEntity3.setNo("");
                        pickResultEntity3.setName("");
                    }
                } else {
                    pickResultEntity2.setNo("");
                    pickResultEntity3.setName("");
                    pickResultEntity3.setNo("");
                    pickResultEntity3.setName("");
                }

                recycler3.setVisibility(View.GONE);
                if (position == 0) {
                    recycler2.setVisibility(View.GONE);
                    return;
                }
                setData2(list.get(position).getChildren());
                recycler2.setVisibility(View.VISIBLE);
            }
        });

        pickAdapter2.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                pickResultEntity2.setNo(entity.getNo());
                pickResultEntity2.setName(entity.getName());
                if (list.get(currentPosition).getChildren().get(position).getChildren() != null){
                    pickResultEntity3.setNo(list.get(currentPosition).getChildren().get(position).getChildren().get(0).getNo());
                    pickResultEntity3.setName(list.get(currentPosition).getChildren().get(position).getChildren().get(0).getName());
                }
                if (position == 0) {
                    recycler3.setVisibility(View.GONE);
                    return;
                }
                setData3(list.get(currentPosition).getChildren().get(position).getChildren());
                recycler3.setVisibility(View.VISIBLE);
            }
        });

        pickAdapter3.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                pickResultEntity3.setNo(entity.getNo());
                pickResultEntity3.setName(entity.getName());
            }
        });

        pickResultEntity1 = new PickResultEntity();
        pickResultEntity2 = new PickResultEntity();
        pickResultEntity3 = new PickResultEntity();
    }

    /*
     * 三级联动数据(初始位置内容填充)
     * */
    public void setThreeLevelData(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>> entityLists = new PickFilterEntity<>();
        List<PickFilterEntity<List<PickFilterEntity>>> entityList = new ArrayList<>();
        PickFilterEntity<List<PickFilterEntity>> pickFilterEntity = new PickFilterEntity<>();
        List<PickFilterEntity> pickFilterEntityList = new ArrayList<>();
        PickFilterEntity entity = new PickFilterEntity();
        entity.setNo("");
        entity.setName(all);
        pickFilterEntityList.add(entity);
        pickFilterEntity.setNo("");
        pickFilterEntity.setName(all);
        pickFilterEntity.setChildren(pickFilterEntityList);
        entityList.add(pickFilterEntity);
        entityLists.setNo("");
        entityLists.setName(all);
        entityLists.setChildren(entityList);
        data.add(0, entityLists);
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                continue;
            }
            List<PickFilterEntity<List<PickFilterEntity>>> list = data.get(i).getChildren();
            if (list != null) {
                for (int k = 0; k < list.size(); k++) {
                    if (list.get(k).getChildren() == null) {
                        list.get(k).setChildren(pickFilterEntityList);
                    } else {
                        list.get(k).getChildren().add(0, entity);
                    }
                }
                list.add(0, pickFilterEntity);
            } else {
                data.get(i).setChildren(entityList);
            }
        }
        setData(data);
    }

    /*
     * 三级联动数据(初始位置内容填充)
     * 仅适用招采劳务分包工程分类三级联动数据(已添加每列"全部"初始数据)
     * */
    public void setThreeLevelData1(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        List<PickFilterEntity<List<PickFilterEntity>>> entityList = new ArrayList<>();
        PickFilterEntity<List<PickFilterEntity>> pickFilterEntity = new PickFilterEntity<>();
        List<PickFilterEntity> pickFilterEntityList = new ArrayList<>();
        PickFilterEntity entity = new PickFilterEntity();
        entity.setNo("");
        entity.setName(all);
        pickFilterEntityList.add(entity);
        pickFilterEntity.setNo("");
        pickFilterEntity.setName(all);
        pickFilterEntity.setChildren(pickFilterEntityList);
        entityList.add(pickFilterEntity);
        for (int i = 0; i < data.size(); i++) {
            List<PickFilterEntity<List<PickFilterEntity>>> list = data.get(i).getChildren();
            if (list == null || list.size() == 0) {
                data.get(i).setChildren(entityList);
            } else {
                for (int k = 0; k < list.size(); k++) {
                    List<PickFilterEntity> children = list.get(k).getChildren();
                    if (children == null || children.size() == 0) {
                        list.get(k).setChildren(pickFilterEntityList);
                    }
                }
            }
        }
        setData(data);
    }

    public void setThreeLevelDataForNormal(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data, String industry, String category, String workType) {
        industry = industry == null ? "" : industry;
        category = category == null ? "" : category;
        workType = workType == null ? "" : workType;

        List<PickFilterEntity<List<PickFilterEntity>>> twoLevelList = new ArrayList<>();
        List<PickFilterEntity> threeLevelList = new ArrayList<>();
        PickFilterEntity entity = new PickFilterEntity();
        entity.setNo("");
        entity.setName(all);
        data.add(0, entity);
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                continue;
            }
            if (data.get(i).getChildren() == null) {
                data.get(i).setChildren(new ArrayList<>());
            }
            data.get(i).getChildren().add(0, entity);
            List<PickFilterEntity<List<PickFilterEntity>>> list = data.get(i).getChildren();
            for (int k = 0; k < list.size(); k++) {
                if (k == 0) {
                    continue;
                }
                if (data.get(i).getChildren().get(k).getChildren() == null) {
                    data.get(i).getChildren().get(k).setChildren(new ArrayList<>());
                }
                data.get(i).getChildren().get(k).getChildren().add(0, entity);

                if (!TextUtils.isEmpty(category) && category.equals(list.get(k).getNo())) {
                    twoPosition = k;
                }

                for (int i1 = 0; i1 < list.get(k).getChildren().size(); i1++) {
                    if (i1 == 0) {
                        continue;
                    }
                    if (!TextUtils.isEmpty(workType) && workType.equals(list.get(k).getChildren().get(i1).getNo())) {
                        threePosition = i1;
                    }
                }
            }
            if (industry.equals(data.get(i).getNo())) {
                currentPosition = i;
            }
        }
        resetData(data);
    }

    private void resetData(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> list) {
        this.list = list;
        List<PickFilterEntity> pickList = new ArrayList<>();
        for (PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>> pickFilterEntity : list) {
            PickFilterEntity entity = new PickFilterEntity();
            entity.setNo(pickFilterEntity.getNo());
            entity.setName(pickFilterEntity.getName());
            pickList.add(entity);
        }

        List<PickFilterEntity> pickList1 = new ArrayList<>();
        if (list.get(currentPosition).getChildren() != null) {
            for (PickFilterEntity<List<PickFilterEntity>> child : list.get(currentPosition).getChildren()) {
                PickFilterEntity entity = new PickFilterEntity();
                entity.setNo(child.getNo());
                entity.setName(child.getName());
                pickList1.add(entity);
            }
        }

        List<PickFilterEntity> pickList2 = new ArrayList<>();
        if (list.get(currentPosition).getChildren().get(twoPosition).getChildren() != null) {
            for (PickFilterEntity childChild : list.get(currentPosition).getChildren().get(twoPosition).getChildren()) {
                PickFilterEntity entity = new PickFilterEntity();
                entity.setNo(childChild.getNo());
                entity.setName(childChild.getName());
                pickList2.add(entity);
            }
        }
        pickResultEntity1.setNo(list.get(currentPosition).getNo());
        pickResultEntity1.setName(list.get(currentPosition).getName());
        if (list.get(currentPosition).getChildren() != null) {
            pickResultEntity2.setNo(list.get(currentPosition).getChildren().get(twoPosition).getNo());
            pickResultEntity2.setName(list.get(currentPosition).getChildren().get(twoPosition).getName());
        }

        if (list.get(currentPosition).getChildren() != null && list.get(currentPosition).getChildren().get(twoPosition).getChildren() != null) {
            pickResultEntity3.setNo(list.get(currentPosition).getChildren().get(twoPosition).getChildren().get(threePosition).getNo());
            pickResultEntity3.setName(list.get(currentPosition).getChildren().get(twoPosition).getChildren().get(threePosition).getName());
        }

        pickAdapter1.setDatas(pickList);
        pickAdapter1.setDefItem(currentPosition);
        pickAdapter2.setDatas(pickList1);
        pickAdapter2.setDefItem(twoPosition);
        pickAdapter3.setDatas(pickList2);
        pickAdapter3.setDefItem(threePosition);
        recycler1.setVisibility(View.VISIBLE);
        recycler2.setVisibility(View.VISIBLE);
        if (twoPosition == 0) {
            recycler3.setVisibility(View.GONE);
        } else {
            recycler3.setVisibility(View.VISIBLE);
        }
        recycler1.scrollToPosition(currentPosition);
        recycler2.scrollToPosition(twoPosition);
        recycler3.scrollToPosition(threePosition);
    }

    private void setData(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> list) {
        this.list = list;
        List<PickFilterEntity> pickList = new ArrayList<>();
        for (PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>> pickFilterEntity : list) {
            PickFilterEntity entity = new PickFilterEntity();
            entity.setNo(pickFilterEntity.getNo());
            entity.setName(pickFilterEntity.getName());
            pickList.add(entity);
        }
        pickResultEntity1.setNo(list.get(currentPosition).getNo());
        pickResultEntity1.setName(list.get(currentPosition).getName());
        pickResultEntity2.setNo(list.get(currentPosition).getChildren().get(0).getNo());
        pickResultEntity2.setName(list.get(currentPosition).getChildren().get(0).getName());
        pickResultEntity3.setNo(list.get(currentPosition).getChildren().get(0).getChildren().get(0).getNo());
        pickResultEntity3.setName(list.get(currentPosition).getChildren().get(0).getChildren().get(0).getName());
        pickAdapter1.setDatas(pickList);
        pickAdapter1.setDefItem(currentPosition);
        recycler1.setVisibility(View.VISIBLE);
        recycler2.setVisibility(View.GONE);
        recycler3.setVisibility(View.GONE);
        recycler1.scrollToPosition(currentPosition);
        if (currentPosition == 0) {
            return;
        }
        setData2(list.get(currentPosition).getChildren());
        recycler2.setVisibility(View.VISIBLE);
    }

    private void setData2(List<PickFilterEntity<List<PickFilterEntity>>> list) {
        List<PickFilterEntity> pickList = new ArrayList<>();
        for (PickFilterEntity<List<PickFilterEntity>> pickFilterEntity : list) {
            PickFilterEntity entity = new PickFilterEntity();
            entity.setNo(pickFilterEntity.getNo());
            entity.setName(pickFilterEntity.getName());
            pickList.add(entity);
        }
        pickAdapter2.setDatas(pickList);
        pickAdapter2.setDefItem(0);
    }

    private void setData3(List<PickFilterEntity> list) {
        pickAdapter3.setDatas(list);
        pickAdapter3.setDefItem(0);
    }

    public void setDefaultItem(int index) {
        this.currentPosition = index;
        if (list == null) {
            return;
        }
        setData(list);
    }

    private OnResultClick onResultClick;

    public interface OnResultClick {

        void result(Object key1, Object key2, Object key3);

        void reset();
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }
}
